package com.android.fastride.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.fastride.R;

public class TopBar extends RelativeLayout {

    private LinearLayout leftOperator;
    private LinearLayout rightOperator;
    private TextView titleTextView;
    private OnLeftAndRightClickListener listener;
    private ImageView leftImageView, rightImageView;

    //监听点击事件 //设置监听器
    public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
        this.listener = listener;
    }

    //按钮点击接口
    public interface OnLeftAndRightClickListener {
        void OnLeftButtonClick();

        void OnRightButtonClick();
    }

    //设置左边按钮的可见性
    public void setLeftButtonVisibility(boolean flag) {
        if (flag)
            leftOperator.setVisibility(View.VISIBLE);
        else
            rightOperator.setVisibility(View.GONE);
    }

    //设置右边按钮的可见性
    public void setRightButtonVisibility(boolean flag) {
        if (flag)
            leftOperator.setVisibility(View.VISIBLE);
        else
            rightOperator.setVisibility(View.GONE);
    }

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {

        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.topbar, this);
        rightOperator = (LinearLayout) findViewById(R.id.right_operate);
        leftOperator = (LinearLayout) findViewById(R.id.left_operate);
        titleTextView = (TextView) findViewById(R.id.title_text);
        leftImageView = (ImageView) findViewById(R.id.iv_left);
        rightImageView = (ImageView) findViewById(R.id.iv_right);
        leftOperator.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnLeftButtonClick();
                    //点击回调
                }
            }
        });
        rightOperator.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnRightButtonClick();
                    //点击回调
                }
            }
        });
        //获得自定义属性并赋值
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        int leftImageViewBackground = typeArray.getResourceId(R.styleable.TopBar_leftBackground,
                -1);
        int rightImageVieewBackground = typeArray.getResourceId(R.styleable
                .TopBar_rightBackground, -1);
        String titleText = typeArray.getString(R.styleable.TopBar_titleText);
        float titleTextSize = typeArray.getDimension(R.styleable.TopBar_titleSize, -1.0f);
        int titleTextColor = typeArray.getColor(R.styleable.TopBar_titleColor, -1);
        //释放资源
        typeArray.recycle();
        if (leftImageViewBackground != -1) {
            leftImageView.setBackgroundResource(leftImageViewBackground);
        }
        if (rightImageVieewBackground != -1) {
            rightImageView.setBackgroundResource(rightImageVieewBackground);
        }
        if (titleText != null) {
            titleTextView.setText(titleText);
        }
        if (titleTextSize != -1.0f) {
            titleTextView.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        }
        if (titleTextColor != -1) {
            titleTextView.setTextColor(titleTextColor);
        }
    }

}
